<?php

namespace app\admin\controller\vppz;

use app\common\controller\Backend;
//use think\addons\Service;
//use think\Exception;

use think\Session;
use app\admin\model\vppz\App;

use app\admin\model\Admin as AdminModel;
/**
 * 系统配置
 *
 * @icon fa fa-gears
 */
class Followup extends Base
{
	protected $branch = 0;	// 0：插件市场版，1：自主授权版

    protected $model = null;

   protected $searchFields = ['id'];

    public function _initialize()
    {
        parent::_initialize();
        $order_id = input('ids',0);
        $this->assign('order_id',$order_id);
        $admin = Session::get("admin");
        $this->assignconfig('order_id',$order_id);
        $this->assign('admin',$admin);
        $this->model = new \app\admin\model\vppz\Followup;
    }
     public function __get($name)
    {
        return Session::get('admin.' . $name);
    }
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        $ids = input('ids');
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->where('order_id','=',$ids)
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('admin')->visible(['username']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->modelValidate = true;
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $form = $this->preExcludeFields($params);
            $result = false;
            try {
                $admin = Session::get("admin");
                $Data = array(
                    'order_id'=>$form['order_id'],
                    'follow_type'=>$form['follow_type'],
                    'follow_content'=>$form['follow_content'],
                    'attachment_url'=>$form['attachment_url'],
                    'operator_id'=>$admin['id'],
                    'operator_name'=>$admin['username']
                );
                $result = $this->model->save($Data);
                if(!$result){
					$this->error('服务器刚刚走神了，重试看看呢');
				}

                if($result){
                    $this->success();
                }else{
                    $this->error('新增记录失败');
                }
            } catch (ValidateException|PDOException|Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $typeList = $this->model->gettypeList();
        $this->assign('typeList',$typeList);
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);

            $typeList = $this->model->gettypeList();
            $this->assign('typeList',$typeList);
            return $this->view->fetch();
        }
        $this->modelValidate = true;
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $form = $this->preExcludeFields($params);
        $result = false;
        try {
            $admin = Session::get("admin");
            $Data = array(
                'follow_type'=>$form['follow_type'],
                'follow_content'=>$form['follow_content'],
                'attachment_url'=>$form['attachment_url'],
                'operator_id'=>$admin['id'],
                'operator_name'=>$admin['username']
            );
            $ret = $this->model->where(['id'=>$row['id']])->update($Data);
            if(!$ret){
                $this->error('服务器刚刚走神了，重试看看呢');
            }

            if($ret){
                    $this->success();
                }else{
                    $this->error('编辑记录失败');
                }
        } catch (ValidateException|PDOException|Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success();

    }

}
